﻿using Reopsitory.AdminRolenInfos;
using Reopsitory.Registers;
using Service.UserManagements.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
using Reopsitory.RoleInfos;
using Microsoft.AspNetCore.Hosting;
using System.IO;

namespace Service.UserManagements
{
    public class UserRoleService : IUserRoleService
    {
        private readonly IRoleReopsitory roleReopsitory;
        private readonly IAdminRoleReopsitory adminRoleReopsitory;
        private readonly IRegisterReopsitory registerReopsitory;
        private readonly IWebHostEnvironment environment;

        public UserRoleService(IRoleReopsitory roleReopsitory, IAdminRoleReopsitory adminRoleReopsitory, IRegisterReopsitory registerReopsitory, IWebHostEnvironment environment)
        {
            this.roleReopsitory = roleReopsitory;
            this.adminRoleReopsitory = adminRoleReopsitory;
            this.registerReopsitory = registerReopsitory;
            this.environment = environment;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="userRoleDto"></param>
        /// <returns></returns>
        public RestFull<UserRoleDto> Insert(UserRoleDto userRoleDto)
        {
            if (!registerReopsitory.Queryable().Any(x=>x.UserName==userRoleDto.UserName && x.Id != userRoleDto.Id) )
            {
                UserInfo user = new UserInfo();

                user.UserPIc = userRoleDto.UserPIc;
                user.UserName = userRoleDto.UserName;
                user.Phone = userRoleDto.Phone;
                user.Password = userRoleDto.Password;
                user.Id = userRoleDto.Id;



                registerReopsitory.Insert(user);

                AdminRoleInfo adminRoleInfo = new AdminRoleInfo();
                adminRoleInfo.Id = user.Id;
                adminRoleInfo.RoleId = userRoleDto.RoleId;

                adminRoleReopsitory.Insert(adminRoleInfo);

                return new RestFull<UserRoleDto> { Code = 1, Message = "添加成功" };
            }
            else
            {
                return new RestFull<UserRoleDto> { Code = 0, Message = "用户名重复" };
            }

        }

        /// <summary>
        /// 查询 显示 分页
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="sDate"></param>
        /// <param name="eDate"></param>
        /// <returns></returns>
        public RestFull<UserRoleDto> Query(string userName, DateTime? sDate, DateTime? eDate, int page, int size)
        {

            var data = registerReopsitory.Queryable().Join(adminRoleReopsitory.Queryable(), a => a.Id, b => b.Id, (a, b) => new
            {
                a.Id,
                a.UserName,
                a.UserPIc,
                a.Phone,
                a.register,
                b.RoleId,
                b.AdminRoleId
            }).Select(x => new UserRoleDto
            {
                UserName = x.UserName,
                Phone = x.Phone,
                register = x.register,
                Id = x.Id,
                AdminRoleId = x.AdminRoleId,
                UserPIc = x.UserPIc,
                RoleId = x.RoleId,
                RoleName = roleReopsitory.Queryable().Where(t => x.RoleId.Contains(t.RoleId.ToString())).Select(t => t.RoleName).ToList(),
            }).ToList();

            if (!string.IsNullOrEmpty(userName))
            {
                data = data.Where(x => x.UserName.Contains(userName)).ToList();
            }
            if (sDate != null)
            {
                data = data.Where(x => x.register >= sDate).ToList();
            }
            if (eDate != null)
            {
                data = data.Where(x => x.register <= eDate).ToList();
            }

            int total = data.Count();
            data = data.Skip((page - 1) * size).Take(size).ToList();




            return new RestFull<UserRoleDto> { Data = data, TotalCount = total, PageSize = size };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="userRoleDto"></param>
        /// <returns></returns>
        public RestFull Delete(int id, int adminRoleId)
        {          
            var userInfo = registerReopsitory.QueryFirst(id);

            var imgPath = $"{environment.WebRootPath}{userInfo.UserPIc}";
            if (File.Exists(imgPath))
            {
                File.Delete(imgPath);
            }
            var user = registerReopsitory.Delete(id);


            var adminrole = adminRoleReopsitory.Delete(adminRoleId);

            if (user == 0 && adminrole == 0)
            {
                return new RestFull { Code = 1, Message = "删除失败" };
            }
            else
            {
                return new RestFull { Code = 0, Message = "删除成功" };
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public RestFull DeleteAll(int[] ids)
        {
            var data = registerReopsitory.QueryList(x => ids.Contains(x.Id));

            foreach (var item in data)
            {
                registerReopsitory.Delete(item.Id);
            }

            var list = adminRoleReopsitory.QueryList(x => ids.Contains(x.Id));

            foreach (var item in list)
            {
                adminRoleReopsitory.Delete(item.AdminRoleId);
            }

            return new RestFull { Code = 1, Message = "删除成功" };
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="userRoleDto"></param>
        /// <returns></returns>
        public UserRoleDto QueryFirst(int id, int adminRoleId)
        {
            var user = registerReopsitory.QueryFirst(id);

            var adminRole = adminRoleReopsitory.QueryFirst(adminRoleId);

            return new UserRoleDto
            {
                UserName = user.UserName,
                Phone = user.Phone,
                UserPIc = user.UserPIc,
                Password = user.Password,
                Id = user.Id,
                AdminRoleId = adminRole.AdminRoleId,
                RoleId = adminRole.RoleId,
            };
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="userRoleDto"></param>
        /// <returns></returns>
        public RestFull<UserRoleDto> Update(UserRoleDto userRoleDto)
        {
            var data = registerReopsitory.QueryList(x => x.UserName == userRoleDto.UserName).FirstOrDefault();

            if (data == null)
            {
                UserInfo user = new UserInfo();

                user.UserPIc = userRoleDto.UserPIc;
                user.UserName = userRoleDto.UserName;
                user.Phone = userRoleDto.Phone;
                user.Password = userRoleDto.Password;
                user.Id = userRoleDto.Id;



                registerReopsitory.Update(user);

                AdminRoleInfo adminRoleInfo = new AdminRoleInfo();
                adminRoleInfo.Id = user.Id;
                adminRoleInfo.RoleId = userRoleDto.RoleId;
                adminRoleInfo.AdminRoleId = userRoleDto.AdminRoleId;

                adminRoleReopsitory.Update(adminRoleInfo);

                return new RestFull<UserRoleDto> { Code = 1, Message = "修改成功" };
            }
            else
            {
                return new RestFull<UserRoleDto> { Code = 0, Message = "用户名重复" };
            }

        }
    }
}
